package com.css.dzj.dzjportal.sys.user.controller;

import com.css.dzj.dzjportal.common.annotation.SysLog;
import com.css.dzj.dzjportal.common.util.Constant;
import com.css.dzj.dzjportal.common.util.R;
import com.css.dzj.dzjportal.common.util.userinfo.UserInfoService;
import com.css.dzj.dzjportal.common.validator.ValidatorUtils;
import com.css.dzj.dzjportal.sys.user.entity.User;
import com.css.dzj.dzjportal.sys.user.form.RegisterForm;
import com.css.dzj.dzjportal.sys.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户注册
 *
 * @author liyuan
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    LoginController loginController;

    /**
     * 用户注册
     *
     * @param form
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody RegisterForm form, HttpServletResponse response) {
        try {
            HttpSession session = request.getSession();
            if (session == null || session.getAttribute("token") == null) {
                // 注册时session为空或者token为空时，游客先登录
                loginController.loginYK(response);
            }
            Map<String, String> map = new HashMap();
            // 表单校验
            ValidatorUtils.validateEntity(form);
            // 根据用户名查找是否重复
            Boolean userExist = userService.isUserExist(form.getUserName());
            if (userExist) {
                return R.error("用户名已存在!");
            }
            // 判断base用户名是否存在
            boolean flag = userInfoService.checkUserExist(form.getUserName(), request);
            if (flag) {
                return R.error("base用户名已存在");
            } else {
                if (!StringUtils.isEmpty(form.getUserName())) {
                    String loginName = form.getUserName();
                    map.put("loginName", loginName);
                } else {
                    return R.error("用户名不能为空");
                }
                if (!StringUtils.isEmpty(form.getPassword())) {
                    String password = form.getPassword();
                    map.put("password", password);
                } else {
                    return R.error("密码不能为空");
                }
                String shehui = "shehui";

                //Map msg = baseService.add(construct.getUserName(), jianshe, construct.getPassword());
                // 新base注册
                R r = userInfoService.register(form.getUserName(), form.getPassword(), shehui, request);
                if ("500".equals(r.getCode())) {
                    return R.error(r.getMsg());
                }

                User user = new User();
                user.setUserName(form.getUserName());
                // 对用户密码进行加密
                user.setPassword(form.getPassword());
                user.setEmail(form.getEmail());
                user.setMobile(form.getMobile());
                user.setCreateTime(new Date());
                user.setDelFlag(Constant.NO_DELETE);
                // 新增用户
                userService.save(user);
                loginController.loginOut(response);
                return R.ok();
            }
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }
}
